//
//  FZDiscoverSearchView.swift
//  FZSina_Swift
//
//  Created by 李忠 on 7/23/16.
//  Copyright © 2016 FZLZ. All rights reserved.
//

import UIKit

class FZDiscoverSearchView: UIView {
    
    override init(frame: CGRect) {
//        super.init(frame: frame)
        super.init(frame: CGRect(x: 0, y: 0, width: SCREENW, height: 44))
        
        setupUI()
        setupLayout()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        
        addSubview(cancelButton)
        addSubview(searchTextField)
        
    }
    func setupLayout() {
        
        cancelButton.snp_makeConstraints { (make) in
            make.centerY.equalTo(self)
            make.size.equalTo(CGSizeMake(50, self.frame.size.height))
            make.trailing.equalTo(self)
        }
        
        searchTextField.snp_makeConstraints { (make) in
            make.top.bottom.leading.equalTo(0)
            make.trailing.equalTo(0)
        }
        
    }
//MARK:==========================事件==========================
    func cancelClickButton(sender: UIButton) {
        
        searchTextField.snp_updateConstraints { (make) in
            make.trailing.equalTo(0)
        }
        UIView.animateWithDuration(0.5, animations: {
            self.layoutIfNeeded()
            }) { (_) in
                self.searchTextField.resignFirstResponder()
        }
    }
//MARK:==========================懒加载==========================
    lazy var searchTextField: UITextField = {
        
        let textField: UITextField = UITextField()
        textField.leftView = UIImageView(imgNamed: "tabbar_discover")
        textField.leftViewMode = .Always
        textField.leftView?.frame.size = CGSizeMake(44, 44)
        textField.leftView?.contentMode = .Center
        textField.backgroundColor = UIColor.whiteColor()
        textField.layer.borderColor = UIColor.orangeColor().CGColor
        textField.delegate = self
        textField.layer.borderWidth = 2
        
        return textField
    }()
    
    lazy var cancelButton: UIButton = {
        
        let btn: UIButton = UIButton()
        btn.addTarget(self, action: #selector(cancelClickButton(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        
        btn.setTitle("取消", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
    
        return btn
    }()
}


extension FZDiscoverSearchView: UITextFieldDelegate{
    func textFieldDidBeginEditing(textField: UITextField) {
        searchTextField.snp_updateConstraints { (make) in
            make.trailing.equalTo(-50)
        }
        
        UIView .animateWithDuration(0.5) { 
            self.layoutIfNeeded()
        }
    }
}